<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<link type="text/css" rel="stylesheet" href="../css/login.css" />
	</head>
	<body >
		<div class="login">
			<div class="login_box">
				<div class="form_item">
					<label class="icon-my text-danger" for="username"></label>
					<div class="input_box">
						<input type="number" id="id" placeholder="userId (4001)">
						<span class="icon_delete icon-delete"></span>
					</div>
				</div>
				<div class="form_item">
					<label class="icon-password text-success" for="password"></label>
					<div class="input_box">
						<input type="text" id="zh" placeholder="请输入昵称">
						<span class="icon_delete icon-delete"></span>
					</div>
				</div>
			</div>
			<div class="remember_login">
				<input id="checkbox" type="checkbox" checked>
				<label for="checkbox">记住密码</label>
			</div>
			<div class="login_btn">
				<a id="login-btn" class="btn"  onclick="login() ">登 录</a>
			</div>
			<div class="other__login" style="padding-top:1rem ">
				<h2><span>其他登录方式</span></h2>
				<ul>
					<li style="width: 33.3%;">
						<a class="icon-weibo"></a>
					</li>
					<li style="width: 33.3%;">
						<a class="icon-qq"></a>
					</li>
					<li style="width: 33.3%;">
						<a class="icon-weixin"></a>
					</li>
				</ul>
			</div>
	</body>
	<script type="text/javascript" src="../script/api.js" ></script>
	<script type="text/javascript" src="../script/sha1.min.js"></script>
	<script type="text/javascript">
		apiready = function() {
		}
		function login() {
			var zh = $api.val($api.byId('zh'));
			var id = $api.val($api.byId('id'));
			if (id == 3001) {
				var tx = '../res/tx1.jpg';
			} else if (id == 3002) {
				var tx = '../res/tx2.jpg';
			} else if (id == 3003) {
				var tx = '../res/tx3.jpg';
			} else if (id == 3004) {
				var tx = '../res/tx4.jpg';
			} else if (id == 3005) {
				var tx = '../res/tx5.jpg';
			} else if (id == 3006) {
				var tx = '../res/tx6.jpg';
			} else if (id == 3007) {
				var tx = '../res/tx7.jpg';
			} else if (id == 3008) {
				var tx = '../res/tx8.jpg';
			} else if (id == 3009) {
				var tx = '../res/tx9.jpg';
			} else if (id == 3018) {
				var tx = '../res/tx0.jpg';
			} else {
				var tx = '../res/tx00.jpg';
			}
			if (zh.length < 2 || zh.length > 20) {
				api.toast({
					msg : '昵称长度为2-20字符',
					duration : 3000,
					location : 'bottom'
				});
			} else if (id.length < 2 || id.length > 5) {
				api.toast({
					msg : 'id长度为4个字符 如4001',
					duration : 3000,
					location : 'bottom'
				});
			} else {
				//获取token
				var userId = id;
				//会员id
				var name = zh;
				//会员昵称
				var portraitUri = tx;
				//会员头像
				var appKey = "pvxdm17jpofbr";
				var appSecret = "QEz3jusf7VW";
				var nonce = Math.floor(Math.random() * 1000000);
				//随机数
				var timestamp = Date.now();
				//时间戳
				var signature = sha1("" + appSecret + nonce + timestamp);
				//数据签名(通过哈希加密计算)
				api.ajax({
					url : "http://api.cn.ronghub.com/user/getToken.json",
					method : "post",
					headers : {
						"RC-App-Key" : appKey,
						"RC-Nonce" : "" + nonce,
						"RC-Timestamp" : "" + timestamp,
						"RC-Signature" : "" + signature,
						"Content-Type" : "application/x-www-form-urlencoded"
					},
					data : {
						values : {
							userId : userId,
							name : name,
							portraitUri : portraitUri
						}
					}
				}, function(ret, err) {
					if (ret) {
						console.log(ret.token)
						$api.setStorage('token', ret.token);
						$api.setStorage('zh', zh);
						$api.setStorage('id', id);
						$api.setStorage('tx', tx);
						api.openWin({
							reload : true,
							name : 'index',
							url : 'main.html',
							hScrollBarEnabled : false,
						});
						setTimeout(function() {
							api.toast({
								msg : '登录超时，请检查网络',
								duration : 3000,
								location : 'bottom'
							});
						}, 5000)
					} else {
						api.toast({
							msg : '获取token失败',
							duration : 3000,
							location : 'bottom'
						});
					}
				});
			}
		}
	</script>
</html>